<template>
  <!-- 小店详情-商品分析159/165 -->
  <div class="goodsAnalysisShop">
    <div class="vip_pay_box" v-if="!vipRootObj[159][165].owner">
      <div class="vip_bg_box">
        <div class="vip_bg_mask"></div>
      </div>
      <div class="vip_box_wrap">
        <payVip
          routerName="FansPortrait"
          :lowestVipNum="vipRootObj[159][165].ownerMinLevel"
        />
      </div>
    </div>
    <template v-else>
      <div class="top">
        <radio_group01
          :radio_values="['近7天', '30天', '90天']"
          v-model="KindName"
          class="tab01"
        />
        <div class="optionsBox">
          <el-date-picker
            class="long_time"
            v-model="liveTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            prefix-icon="el-icon-arrow-down"
            unlink-panels
            :clearable="false"
            :picker-options="pickeroptions"
            value-format="yyyy-MM-dd"
            @change="onPick1"
          ></el-date-picker>
        </div>
      </div>
      <!-- 添加商品品类销售额占比及分布 -->
      <div class="chart_box">
        <div>
          <p class="title">商品品类销售额占比</p>
          <div>
            <BigPie
              v-if="!loading2 && categoryVolume.length"
              :valueData="categoryVolume"
            />
            <div
              v-loading="loading2"
              v-if="loading2"
              class="loading"
              element-loading-text="拼命加载中..."
              element-loading-spinner="el-icon-loading"
            ></div>
            <div class="zw" v-if="!loading2 && !categoryVolume.length">
              <img src="@/assets/liu/zw.png" />
              <p>暂无数据</p>
            </div>
          </div>
        </div>
        <div>
          <p class="title">商品销售额分布</p>
          <div>
            <BigPie
              v-if="!loading3 && liveVideoVolume.length"
              :valueData="liveVideoVolume"
            />
            <div
              v-loading="loading3"
              v-if="loading3"
              class="loading"
              element-loading-text="拼命加载中..."
              element-loading-spinner="el-icon-loading"
            ></div>
            <div class="zw" v-if="!loading3 && !liveVideoVolume.length">
              <img src="@/assets/liu/zw.png" />
              <p>暂无数据</p>
            </div>
          </div>
        </div>
      </div>
      <p class="title">商品列表</p>
      <template>
        <div class="par_top">
          <dl>
            <dt>商品类目</dt>
            <dd
              ref="ovBox"
              :class="[{ overstep: isOverstep }, { is_unfold: isUnfold }]"
            >
              <div class="cg_items" ref="ovChild">
                <span
                  v-for="(item, idx) in categoryList"
                  :key="idx + 'cdi'"
                  :class="{ active: item.name == isActive }"
                  @click="getCondition(item)"
                  >{{ item.name }}</span
                >
                <i @click="unfoldFn">{{ isUnfold ? "收起" : "展开" }}</i>
              </div>
            </dd>
          </dl>
          <radio_group
            :radio_values="[
              '不限',
              '0-200',
              '200-500',
              '500-1k',
              '1k-2k',
              '2k-4k',
              '4k-6k',
              '6k-8k',
              '8k-1w',
              '1w以上',
            ]"
            v-model="my_radio01"
            title="价格区间"
            isSearch="1"
          />
        </div>
        <div class="searchBox">
          <el-input
            type="text"
            v-model.trim="keyWords"
            @keyup.enter.native="toSearch"
            placeholder="请输入商品关键词搜索"
          ></el-input>
          <div class="imgBox" @click="toSearch">
            <!-- <img src="@/assets/wang/talentCompare/search.png" /> -->
          </div>
        </div>
        <div class="tab_box">
          <table>
            <thead>
              <tr>
                <th>商品</th>
                <th>售价</th>
                <th>佣金率</th>
                <th
                  @click="sort_fun(false, 3)"
                  v-if="orderBy == 1 && sort == 3"
                >
                  关联视频
                  <img
                    src="@/assets/meng/down_up.png"
                    v-if="orderBy == 1 && sort == 3"
                    class="sort"
                  />
                  <img src="@/assets/meng/no_down_up.png" v-else />
                </th>
                <th v-else @click="sort_fun(true, 3)">
                  关联视频
                  <img
                    src="@/assets/meng/up_down.png"
                    v-if="orderBy == 0 && sort == 3"
                  />
                  <img src="@/assets/meng/no_down_up.png" v-else />
                </th>
                <th
                  @click="sort_fun(false, 4)"
                  v-if="orderBy == 1 && sort == 4"
                >
                  关联直播
                  <img
                    src="@/assets/meng/down_up.png"
                    v-if="orderBy == 1 && sort == 4"
                    class="sort"
                  />
                  <img src="@/assets/meng/no_down_up.png" v-else />
                </th>
                <th v-else @click="sort_fun(true, 4)">
                  关联直播
                  <img
                    src="@/assets/meng/up_down.png"
                    v-if="orderBy == 0 && sort == 4"
                  />
                  <img src="@/assets/meng/no_down_up.png" v-else />
                </th>
                <th>关联达人</th>
                <th
                  @click="sort_fun(false, 1)"
                  v-if="orderBy == 1 && sort == 1"
                >
                  销量
                  <img
                    src="@/assets/meng/down_up.png"
                    v-if="orderBy == 1 && sort == 1"
                    class="sort"
                  />
                  <img src="@/assets/meng/no_down_up.png" v-else />
                </th>
                <th v-else @click="sort_fun(true, 1)">
                  销量
                  <img
                    src="@/assets/meng/up_down.png"
                    v-if="orderBy == 0 && sort == 1"
                  />
                  <img src="@/assets/meng/no_down_up.png" v-else />
                </th>
                <th
                  @click="sort_fun(false, 2)"
                  v-if="orderBy == 1 && sort == 2"
                >
                  销售额
                  <img
                    src="@/assets/meng/down_up.png"
                    v-if="orderBy == 1 && sort == 2"
                    class="sort"
                  />
                  <img src="@/assets/meng/no_down_up.png" v-else />
                </th>
                <th v-else @click="sort_fun(true, 2)">
                  销售额
                  <img
                    src="@/assets/meng/up_down.png"
                    v-if="orderBy == 0 && sort == 2"
                  />
                  <img src="@/assets/meng/no_down_up.png" v-else />
                </th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item, index) in table_data01" :key="'table' + index">
                <td>
                  <div @click="toGoodsDetails(item.proId, '0')">
                    <img :src="item.proLogo" v-errorImg:shop />
                  </div>
                  <div @click="toGoodsDetails(item.proId, '0')">
                    <div class="goods_iden" v-if="item.proSign">
                      {{ item.proSign }}
                    </div>
                    <p>{{ item.proTitle }}</p>
                  </div>
                </td>
                <td>￥{{ item.proPrice }}</td>
                <!-- 佣金 -->
                <td v-if="item.commissionRate && item.commissionRate < 1">
                  {{ formatRate(item.commissionRate, 1) }}
                </td>
                <td v-else>--</td>
                <td>{{ format_num(item.associatedVideo) }}</td>
                <td>{{ format_num(item.associatedlive) }}</td>
                <td>{{ format_num(item.associatedAuthor) }}</td>
                <td>{{ format_num(item.volume) }}</td>
                <td>{{ format_num(item.amount) }}</td>
              </tr>
            </tbody>
          </table>
          <div
            v-loading="loading"
            v-if="loading"
            class="loading"
            element-loading-text="拼命加载中..."
            element-loading-spinner="el-icon-loading"
          ></div>
          <div class="zw" v-if="!table_data01.length && !loading">
            <img src="@/assets/liu/zw.png" />
            <p>暂无数据</p>
          </div>
        </div>
      </template>
      <!-- <div
        v-loading="loading"
        v-if="categoryList.length<2"
        class="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
      ></div>
      <div class="zw" v-if="categoryList.length<2&&!loading">
        <img src="@/assets/liu/zw.png" />
        <p>暂无数据</p>
      </div>-->
      <div class="pageBox" v-if="total > 9 && !loading">
        <el-pagination
          background
          layout="prev, pager, next, jumper"
          :total="total"
          :page-size="size"
          :current-page="page"
          @current-change="changePage"
          :hide-on-single-page="true"
        ></el-pagination>
        <!-- :hide-on-single-page="true" -->
      </div>
    </template>
  </div>
</template>

<script>
import SourceIcon from "@/component/SourceIcon.vue"; //图标
import BigPie from "@/pages/wang/talent/tdetails/component/BigPie";
export default {
  props: [],
  data() {
    return {
      loading2: false,
      loading3: false,
      categoryList: [],
      total: 0,
      table_data01: [],
      cancel01: null,
      Price: 0,
      page: 1,
      size: 9,
      loading: false,
      orderBy: 1,
      sort: 2,
      keyWords: "",
      my_radio01: "不限",
      isActive: "全部",
      isOverstep: false, //是否超出（初始化）
      isUnfold: false, //是否展示
      KindName: "30天",
      liveTime: [],
      startDate: "",
      endDate: "",
      // 选择时间 不可选的日期.
      pickeroptions: {
        disabledDate: (time) => {
          return (
            time.getTime() < Date.now() - 180 * 24 * 60 * 60 * 1000 ||
            time.getTime() > Date.now() - 24 * 60 * 60 * 1000
          ); // 返回 所有时间 大于 当前时间
        },
      },
      vipRootObj: this.$store.state.userRoot, //会员权限对象
      categoryVolume: [], //商品品类销售额占比
      liveVideoVolume: [], //商品销售额分布
    };
  },
  components: { SourceIcon, BigPie },
  computed: {},
  created() {},
  mounted() {
    this.setCondition();
    if (this.vipRootObj[159][165].owner) {
      this.shopprodate();
      this.shopCategoryAPI();
    }
  },
  watch: {
    // 搜索
    keyWords: function (newVal, oldVal) {
      if (newVal == "") {
        this.table_data01 = [];
        this.shopprodate();
      }
    },
    // 时间选择器
    KindName: function (newVal, oldVal) {
      this.page = 1;
      this.table_data01 = [];
      if (this.KindName != "") {
        this.KindName = newVal;
        this.shopprodate();
        this.shopCategoryAPI();
      }
    },
    isActive() {
      this.table_data01 = [];
      this.shopprodate();
    },
    my_radio01(newValue) {
      this.page = 1;
      this.table_data01 = [];
      if (newValue == "不限") {
        this.Price = 0;
      } else if (newValue == "0-200") {
        this.Price = 1;
      } else if (newValue == "200-500") {
        this.Price = 2;
      } else if (newValue == "500-1k") {
        this.Price = 3;
      } else if (newValue == "1k-2k") {
        this.Price = 4;
      } else if (newValue == "2k-4k") {
        this.Price = 5;
      } else if (newValue == "4k-6k") {
        this.Price = 6;
      } else if (newValue == "6k-8k") {
        this.Price = 7;
      } else if (newValue == "8k-1w") {
        this.Price = 8;
      } else if (newValue == "1w以上") {
        this.Price = 9;
      }
      this.shopprodate();
    },
  },
  methods: {
    // 商品品类和销售额分布接口
    shopCategoryAPI() {
      this.loading2 = true;
      this.loading3 = true;
      this.$axios
        .post("/api/product/ShopProClassLiveVideoSalesVolume", {
          startDate: this.startDate,
          endDate: this.endDate,
          shopId: this.$route.params.id,
        })
        .then((res) => {
          if (res.data.code === 0) {
            this.categoryVolume = res.data.data.categoryVolume;
            this.liveVideoVolume = res.data.data.liveVideoVolume;
          }
          this.loading2 = false;
          this.loading3 = false;
        });
    },
    // 跳商品详情
    toGoodsDetails(id, tabNo) {
      let router = this.$router.resolve({
        path: `/talent_main/shop_main/${id}/${tabNo}`,
      });
      window.open(router.href, "_blank");
    },
    shopprodate() {
      this.loading = true;
      this.getTime();
      let that = this;
      // 商品类目
      let CategoryId;
      if (this.categoryList.length) {
        that.categoryList.map((item, index) => {
          if (item.name == that.isActive) {
            CategoryId = item.value;
          }
        });
      }

      this.$axios
        .post(
          "/api/product/shopprodate",
          {
            startDate: this.startDate,
            endDate: this.endDate,
            shopId: this.$route.params.id,
            desc: that.orderBy,
            key: this.keyWords,
            sort: this.sort,
            pageSize: this.size,
            pageIndex: that.page,
            FirstCategory: CategoryId,
            Price: this.Price,
          },
          {
            cancelToken: new that.$axios.CancelToken(function executor(c) {
              if (that.cancel01) that.cancel01();
              that.cancel01 = c;
            }),
          }
        )
        .then((response) => {
          this.loading = false;
          this.total = 0;
          if (response.data.code == 0) {
            this.total = response.data.count;
            this.categoryList = response.data.data.categoryList;
            this.categoryList.unshift({ name: "全部", value: 0 });
            this.table_data01 = response.data.data.list;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 热门直播间分页
    changePage(n) {
      this.page = n;
      this.table_data01 = [];
      this.shopprodate();
    },
    // 排序
    sort_fun(parm01, parm02) {
      if (parm01) {
        this.orderBy = 1;
      } else {
        this.orderBy = 0;
      }
      this.sort = parm02;
      this.table_data01 = [];
      this.shopprodate();
    },
    //搜索
    toSearch() {
      this.page = 1;
      this.table_data01 = [];
      this.shopprodate();
    },
    getCondition(itemObj) {
      this.page = 1;
      //点击达人行业选项
      this.isActive = itemObj.name;
    },
    // 转开收起
    unfoldFn() {
      this.isUnfold = !this.isUnfold;
    },
    setCondition() {
      this.$nextTick(() => {
        let box = this.$refs.ovBox;
        if (!box) return;
        let childBox = this.$refs.ovChild;
        let boxHeight = box.offsetHeight;
        let childBoxHeight = childBox.offsetHeight;
        if (boxHeight < childBoxHeight) {
          this.isOverstep = true;
        }
      });
    },
    onPick1() {
      this.KindName = "";
      this.page = 1;
      this.table_data01 = [];
      this.shopprodate();
      this.shopCategoryAPI();
    },
    getTime() {
      switch (this.KindName) {
        case "近7天":
          this.endDate = this.timestamp(
            Date.now() / 1000 - 24 * 60 * 60,
            "Y-M-D"
          );
          this.startDate = this.timestamp(
            Date.now() / 1000 - 86400 * 7,
            "Y-M-D"
          );
          break;

        case "30天":
          this.endDate = this.timestamp(
            Date.now() / 1000 - 24 * 60 * 60,
            "Y-M-D"
          );
          this.startDate = this.timestamp(
            Date.now() / 1000 - 86400 * 30,
            "Y-M-D"
          );
          break;
        case "90天":
          this.endDate = this.timestamp(
            Date.now() / 1000 - 24 * 60 * 60,
            "Y-M-D"
          );
          this.startDate = this.timestamp(
            Date.now() / 1000 - 86400 * 90,
            "Y-M-D"
          );
          break;
        default:
          this.startDate = this.liveTime[0];
          this.endDate = this.liveTime[1];
          break;
      }
      this.liveTime = [this.startDate, this.endDate];
    },
  },
};
</script>

<style lang="less" scoped>
.goodsAnalysisShop {
  padding-left: 20px;
  padding-right: 20px;
  width: 100%;
  box-sizing: border-box;
  .top {
    margin-bottom: 10px;
    // width: 100%;
    display: flex;
    justify-content: flex-end;
    .optionsBox {
      margin-left: 10px;
    }
  }
  .chart_box {
    display: flex;

    > div {
      width: 100%;

      > div {
        position: relative;
        height: 260px;
        padding-top: 40px;
        width: 95%;
        margin-bottom: 40px;
      }
    }
  }

  .title {
    font-size: 16px;
    font-weight: 600;
    letter-spacing: 0px;
    color: #222222;
    position: relative;
    text-indent: 9px;
    margin-bottom: 20px;
    &::after {
      content: "";
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 4px;
      height: 16px;
      background-color: #fd7f2c;
      border-radius: 2px;
    }
  }
  dl {
    display: flex;
    font-size: 14px;
    margin-bottom: 6px;
    &:last-child {
      margin-bottom: 0;
    }
    dt {
      padding-top: 6px;
      width: 78px;
      flex-shrink: 0;
      color: #888;
    }
    dd {
      position: relative;
      color: #555;
      height: 32px;
      overflow: hidden;
      .cg_items {
        display: inline-block;
        font-size: 0;
        span {
          display: inline-block;
          height: 32px;
          padding: 0 16px;
          // margin-bottom: 5px;
          line-height: 32px;
          font-size: 14px;
          cursor: pointer;
          vertical-align: middle;
          &.active {
            color: #fd7f2c;
            background-color: #fff1e8;
            border-radius: 4px;
          }
        }
        span:hover {
          color: #ff924b;
        }
        i {
          position: absolute;
          display: none;
          right: 0;
          top: 6px;
          font-size: 14px;
          font-style: normal;
          color: #fd7f2c;
          cursor: pointer;
        }
      }
      &.overstep {
        padding-right: 22px;
        .cg_items {
          i {
            display: inline-block;
            vertical-align: middle;
          }
        }
      }
      &.is_unfold {
        padding-right: 0;
        height: inherit;
        .cg_items {
          i {
            position: inherit;
          }
        }
      }
    }
  }
  // 搜索框
  .searchBox {
    width: 308px;
    height: 32px;
    border-radius: 4px;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid #e5e5e5;
    margin-top: 8px;
    margin-bottom: 10px;
    .el-input__inner {
      flex: 1;
    }
    .imgBox:hover {
      background: url("../../../assets/wang/talentCompare/search.png") no-repeat
          10px 7px,
        #fff;
      background-size: 15px 15px;
    }
    .imgBox {
      width: 40px;
      height: 32px;
      border-radius: 0px 4px 4px 0px;
      cursor: pointer;
      display: grid;
      place-items: center;
      background: url("../../../assets/wang/talentCompare/search-nohover.png")
          no-repeat 10px 7px,
        #fff;
      background-size: 15px 15px;
      img {
        width: 15px;
        height: 15px;
      }
    }
  }
  table {
    width: 100%;
    thead {
      width: 100%;
      tr {
        width: 100%;
        height: 48px;
        background-color: #f5f9ff;
        border-radius: 4px 4px 0px 0px;

        > th {
          cursor: pointer;
          font-size: 14px;
          font-weight: 600;
          color: #555555;
          img {
            vertical-align: initial;
            width: 5px;
            height: 10px;
          }
          &:nth-child(1) {
            cursor: text;
            width: 29%;
            text-align: left;
            padding-left: 46px;
          }
          &:last-child {
            padding-right: 30px;
          }
        }
      }
    }
    tbody {
      width: 100%;
      tr {
        width: 100%;
        height: 78px;
        border-bottom: 1px solid #efefef;
        td {
          height: 78px;
          text-align: center;
          font-family: DINAlternate-Bold;
          font-size: 15px;
          color: #222222;
          &:nth-child(1) {
            padding-left: 36px;
            text-align: left;
          }
          &:last-child {
            padding-right: 30px;
          }
          &:nth-child(1) {
            display: flex;
            > div {
              align-self: center;
              &:nth-child(1) {
                width: 50px;
                height: 50px;
                flex: 0 0 50px;
                margin-right: 10px;
                border-radius: 6px;
                overflow: hidden;
                > img {
                  width: 100%;
                  height: 100%;
                  cursor: pointer;
                }
              }
              &:nth-child(2) {
                flex: 0 0 calc(100% - 70px);
                // width: calc(100% - 50px);
                font-size: 14px;
                font-weight: 600;
                color: #222222;
                position: relative;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 2;
                overflow: hidden;
                #SourceIcon {
                  position: absolute;
                  left: 0;
                }
                p {
                  text-indent: 20px;
                  cursor: pointer;
                  display: inline;
                  word-break: break-all;
                  &:hover {
                    color: #ff924b;
                  }
                }
              }
            }
          }
        }
      }
      .goods_iden {
        padding: 2px 5px;
        text-align: center;
        display: inline-block;
        height: 16px;
        background-color: #fff7ee;
        border-radius: 4px;
        border: solid 1px #fd7f2c;
        font-size: 12px;
        font-weight: normal;
        line-height: 16ppx;
        color: #fd7f2c;
      }
    }
  }
  .tab_box {
    position: relative;
    min-height: 370px;
  }
}
</style>

